package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.ParamService;
import com.leyou.item.service.QuerySpuByPageServie;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private QuerySpuByPageServie querySpuByPage;
    @Autowired
    private SkuService skuService;
    @Autowired
    private ParamService paramService;
    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private QuerySpuByPageServie querySpuByPageServie;


    //分页查询
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id
    ) {

        return ResponseEntity.ok(this.querySpuByPage.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
    }

    //添加商品
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO) {
        this.querySpuByPage.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据ID查询商品
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {

        return ResponseEntity.ok(this.querySpuByPage.queryGoodsById(id));
    }

    //添加商品
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        this.querySpuByPage.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    //修改上下架
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {
        this.querySpuByPage.updateSaleable(id, saleable);
        return ResponseEntity.ok().build();
    }

    //根据id批量查询sku
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(
                this.skuService.querySkuBySpuIds(ids));
    }

     ///根据spuId查询spu的所有规格参数值
     @GetMapping("/spec/value")
     public ResponseEntity<List<SpecParamDTO>> querySpecParamBySpu(
             @RequestParam("id")Long id,
             @RequestParam(value = "searching",required = false)Boolean searching){

         return ResponseEntity.ok(this.querySpuByPageServie.querySpecParamBySpu(id,searching));
     }


    // 根据id查询detail
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(this.spuDetailService.querySpuDetailById(id));
    }

    //根据id查询sku集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id) {

        return ResponseEntity.ok(this.skuService.querySkuBySpuId(id));
    }

    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }

}
